package com.zsc.edu.gateway.modules.system.controller;

import com.zsc.edu.gateway.framework.mybatisplus.DataPermission;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.operationLog.entity.OperationLogAnnotation;
import com.zsc.edu.gateway.modules.system.dto.MenuDto;
import com.zsc.edu.gateway.modules.system.entity.Menu;
import com.zsc.edu.gateway.modules.system.service.MenuService;
import com.zsc.edu.gateway.modules.system.vo.MenuVo;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhuang
 */
@AllArgsConstructor
@RestController
@RequestMapping("/api/rest/menu")
public class MenuController {

    private final MenuService service;

    /**
     * 新建菜单
     */
    @PostMapping
    @PreAuthorize("hasAuthority('system:menu:create')")
    @OperationLogAnnotation(content = "'菜单'", operationType = "新建")
    public Menu create(@RequestBody MenuDto dto) {
        return service.create(dto);
    }

    /**
     * 更新菜单
     */
    @PatchMapping("/{id}")
    @PreAuthorize("hasAuthority('system:menu:update')")
    @OperationLogAnnotation(content = "'菜单'", operationType = "更新")
    public Menu update(@RequestBody MenuDto dto, @PathVariable("id") Long id) {
        return service.update(dto, id);
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('system:menu:delete')")
    @OperationLogAnnotation(content = "'菜单'", operationType = "删除")
    public Boolean delete(@PathVariable("id") Long id) {
        return service.delete(id);
    }

    /**
     * 根据名字返回树
     */
    @DataPermission
    @GetMapping("/tree")
    @PreAuthorize("hasAuthority('system:menu:query')")
    public List<MenuVo> tree(@AuthenticationPrincipal UserDetailsImpl userDetails, @RequestParam String name) {
        return service.getTree(userDetails, name);
    }

    /**
     * 根据ID查询菜单详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('system:menu:query')")
    public Menu detail(@PathVariable("id") Long id) {
        return service.detail(id);
    }
}
